package com.aaa.sso.util;

import com.aaa.common.bo.Member;
import com.aaa.common.util.Result;
import com.aaa.sso.service.RemoteMemberService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class MemberRealm extends AuthorizingRealm {

    @Autowired
    private RemoteMemberService remoteMemberService;

    /**
     * 授权方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 认证方法
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取用户名
        String userName=(String) authenticationToken.getPrincipal();
        //根据用户名获取对象
        Member member = remoteMemberService.getUserByUsername(userName);
        //Member member= (Member) result.getT();
        if (member==null){
            throw  new AccountException();
        }

        //四个参数 登陆后会员信息  加密密码  盐值的字节数组对象  当前验证对象的名称
        return new SimpleAuthenticationInfo(member,member.getPassword(),
                ByteSource.Util.bytes(member.getPassword()),getName());
    }
}
